{% extends "../master.html" %}
{% block hfm_css %}
<style type="text/css">
    .layui-textarea{
        height: 200px;
    }
    .layui-bg-gray{
        top: 5px;
    }

</style>
{% end %}
{% block page_content %}
<div class="layui-fluid">
    <div class="layui-row">
        <form action="" method="post" class="layui-form layui-form-pane">
            <div class="layui-form-item layui-form-input">
                <label for="taskname" class="layui-form-label">
                    <span class="x-red">*</span>任务名称</label>
                <div class="layui-input-block">
                    <input type="text" id="taskname" name="taskname" required="" lay-verify="required" autocomplete="off" class="layui-input" placeholder="任务名称，支持英文、数字和_组成">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label for="site_list" class="layui-form-label">
                    <span class="x-red">*</span> 站点列表
                </label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入标准网站域名, 多个域名可使用回车分隔" id="site_list" name="site_list" class="layui-textarea" required="" lay-verify="required"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px; border-radius: 10px;"><span class="x-red">*</span>任务周期</label>
                <div class="layui-inline">
                    <div class="layui-input-block " style="margin-left:0px">
                        <input type="radio" name="worktime" value="one" checked title="一次">
                        <input type="radio" name="worktime" value="day" title="每天">
                        <input type="radio" name="worktime" value="week" title="每周">
                        <input type="radio" name="worktime" value="month" title="每月">
                        <input type="radio" name="worktime" value="season" title="每季度">
                        <input type="radio" name="worktime" value="halfyear" title="每半年">
                        <input type="radio" name="worktime" value="year" title="每年">
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">
                    <span class="x-red">*</span> 检测策略
                </label>
                <table  class="layui-table layui-input-block">
                    <tbody>
                        <tr>
                            <td>
                                <input type="checkbox" name="text[check]" lay-skin="primary" lay-filter="father" value="文本检测" title="文本检测">
                            </td>
                            <td>
                                <div class="layui-input-block">
                                    <input name="text[]" lay-skin="primary" type="checkbox" value="涉黄" title="涉黄">
                                    <input name="text[]" lay-skin="primary" type="checkbox" value="涉政" title="涉政">
                                    <input name="text[]" lay-skin="primary" type="checkbox" value="涉赌" title="涉赌">
                                    <input name="text[]" lay-skin="primary" type="checkbox" value="涉毒" title="涉毒">
                                    <input name="text[]" lay-skin="primary" type="checkbox" value="涉暴" title="涉暴">
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input name="image[check]" lay-skin="primary" type="checkbox" value="图片检测" title="图片检测" lay-filter="father">
                            </td>
                            <td>
                                <div class="layui-input-block">
                                    <input name="img[]" lay-skin="primary" type="checkbox" value="涉黄" title="涉黄">
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input name="keyword[check]" lay-skin="primary" type="checkbox" value="关键词检测" title="关键词检测" lay-filter="father">
                            </td>
                            <td>
                                <div class="layui-input-block">
                                    {% if keyword_strategy_list %}
                                        {% for keyword_strategy_item in keyword_strategy_list %}
                                            <input name="kw[]" lay-skin="primary" type="checkbox" value="{{ keyword_strategy_item.strategy_name }}" title="{{ keyword_strategy_item.strategy_name }}">
                                        {% end %}
                                    {% end %}
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 150px; border-radius: 10px;"><span class="x-red">*</span>篡改监测</label>
                <div class="layui-inline">
                    <div class="layui-input-block " style="margin-left:0px">
                        <input type="checkbox" lay-skin="switch" lay-text="开|关">
                        <span class="layui-badge layui-bg-gray">开启后会根据任务周期进行实时监测站点是否被篡改</span>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 150px; border-radius: 10px;"><span class="x-red">*</span>可用性监测</label>
                <div class="layui-inline">
                    <div class="layui-input-block " style="margin-left:0px">
                        <input type="checkbox" checked lay-skin="switch" lay-text="开|关">
                        <span class="layui-badge layui-bg-gray">开启后会根据任务周期进行实时监测站点的响应时长，负载压力，是否可达的状态以趋势图显示</span>
                    </div>
                </div>
            </div>
            <div class="layui-row">
                <div class="layui-col-xs6" >
                    <button type="reset" class="layui-btn">重置</button>
                </div>
                <div class="layui-col-xs6" align='right'>
                    <button type="submit" class="layui-btn" lay-submit lay-filter="task_form" >确认</button>
                </div>
             </div>
        </form>
    </div>
</div>
<script type="text/javascript">
layui.use(['form','layer'], function() {
    $ = layui.jquery;
    var form = layui.form
        , layer = layui.layer;

    form.render();

    //自定义验证规则
    form.verify({
        nikename: function (value) {
            if (value.length < 5) {
                return '昵称至少得5个字符啊';
            }
        }
        , pass: [/(.+){6,12}$/, '密码必须6到12位']
        , repass: function (value) {
            if ($('#L_pass').val() != $('#L_repass').val()) {
                return '两次密码不一致';
            }
        }
    });
    //监听提交
    form.on('submit(task_form)', function (data) {
        {#console.log(data);#}
        //发异步，把数据提交给Tronado
        var taskInfo = data.field;
        var postUrl = "/task/add";
        var textArray = [];
        var imgArray = [];
        var kwArray = [];
        // [{'text':[涉黄，涉政，反腐]}, {'img':[涉黄，涉政，反腐]},{'kw':[涉黄，涉政，反腐]}]
        for(var i in taskInfo){
            var re_text = /^text.*/;
            var re_img = /^img.*/;
            var re_kw = /^kw.*/;
            if(re_text.test(i)){
                if(i != "text[check]"){
                    textArray.push(taskInfo[i])
                }
            }else if(re_img.test(i))
            {
                if(i != "img[check]"){
                    imgArray.push(taskInfo[i])
                }
            }else if(re_kw.test(i)){
                if(i != "kw[check]"){
                    kwArray.push(taskInfo[i])
                }
            }
        }
        taskInfo.strategy_text = textArray.toString();
        taskInfo.strategy_img = imgArray.toString();
        taskInfo.strategy_kw = kwArray.toString();
        //console.log(taskInfo);
        $.ajax({
            url: postUrl,
            type: 'post',
            data: taskInfo,
            beforeSend: function () {
                this.layerTaskAdd = layer.load(0, {shade: [0.5, '#393D49']});
            },
            success: function (data) {
                if (data.status == 'error') {
                    layer.msg(data.msg, {icon: 5});
                } else if (data.status == 'success') {
                    layer.msg(data.msg, {
                        icon: 6,
                        time: 2000
                    }, function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                    })
                }
            },
            complete: function () {
                layer.close(this.layerTaskAdd)
            }
        });
        return false;
    });
    form.on('checkbox(father)', function(data){
        if(data.elem.checked){
            $(data.elem).parent().siblings('td').find('input').prop("checked", true);
            form.render();
        }else{
           $(data.elem).parent().siblings('td').find('input').prop("checked", false);
            form.render();
        }
    })
})
</script>
{% end %}